<!-- 注册
 1. vue始终在用双向绑定的思想来准备前后端交互，每一个vue都会定义好变量给 表单（form）或者表格（table）中的html元素的值绑定
-->
<template>
  <div><!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
    <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
    <el-dialog title="标题名" :visible.sync="addDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
      <el-form :model="cheforder" label-width="80px" ref="addCheforderForm">

        <el-form-item label="选择厨师">
          <el-col :span="3">
            <el-select v-model="cheforder.cno" placeholder="请选择">
              <el-option v-for="(chefordergroup,index) in allCheforderGroup" :key="chefordergroup.num" :label="chefordergroup.cName" :value="chefordergroup.num"/>
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item label="金额">
          <el-col :span="2">
            <el-input v-model="cheforder.cuprice"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="状态描述">
          <el-col :span="12">
            <el-input v-model="cheforder.custate"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="订单日期">
          <el-col :span="5">
            <el-date-picker type="date" placeholder="选择日期" v-model="cheforder.cubegindate" style="width: 100%;"></el-date-picker>
          </el-col>
          <el-col class="line" :span="1">-</el-col>
          <el-col :span="5">
            <el-time-picker placeholder="选择时间" v-model="cheforder.cubegintime" style="width: 100%;"></el-time-picker>
          </el-col>
        </el-form-item>
      </el-form>
      <span>
      <el-button type="success" @click="addCheforder('addCheforderForm')">注册</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import api from "../../../../axios/api"

  export default {
    name: "addCheforder",
    data() {
      return {
        cheforder: {},
        addDialogIsShow: false,
        allCheforderGroup: [{num: 7001, "cName": "王大厨"}, {num: 7002, "cName": "张大厨"},{num: 7003, "cName": "李大厨"},{num: 7004, "cName": "赵大厨"},{num: 7005, "cName": "冯大厨"}],
      }
    },
    methods: {
      addCheforder: function (addCheforderForm) {
        this.$refs[addCheforderForm].validate(valid => {//此处refs对象和标签上的ref对应
          if (valid) {
            //如果通过验证 to do...
            //console.log(this.cheforder);
            this.setDataFromAxios();
            this.handleClose();
            this.$message.success("增加成功")
          } else {
            console.log('error submit!!')
            this.$message.error("增加失败")
            return false
          }
        })
      },
      //页面初始化时加载的事件
      init() {
        this.addDialogIsShow = true;
      },

      //关闭页面
      handleClose() {
        this.addDialogIsShow = false;
        //给父组件传参
        this.$emit("addDialogIsShow", this.addDialogIsShow);
      },
      setDataFromAxios: function () {
        console.log(this.cheforder);
        //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
        api.setDataFromAxios(this.$store.state.host + "/cheforderController/addCheforder", this.cheforder, "post")
          .then(res => {
            this.leagues = res.leagues
          });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
      },
    },

    //监听
    watch: {
      dialogisShow(newValue, oldValue) {
        this.addDialogIsShow = newValue;
      }
    }

  }
</script>

<style scoped>

</style>
